<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>user index</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>

<body>
    <p>code:<input id="code" value="" /></p>
    <p>token:<input id="token" value="" /></p>
    <p><a href="javascript:;" onclick="toLogin()">tologin</a></p>
    <p><a href="javascript:;" onclick="getToken()">getToken</a></p>
    <p><a href="javascript:;" onclick="logout()">logout</a></p>
    <!-- <p><a href="http://demo.com/token/logout">token logout</a></p> -->
</body>
<script>
    $(function () {
        $("#code").val(getQueryParam("code"));
    })
    function getQueryParam(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) { return pair[1]; }
        }
        return (false);
    }

    function toLogin() {
        var params = "?client_id=web&response_type=code&redirect_uri=http://user.cn";
        location.href = "http://sso.cn/oauth/authorize" + params;
    }

    function getToken() {
        $.post("http://sso.cn/oauth/token", {
            code: $("#code").val(),
            grant_type: "authorization_code",
            client_id: "web",
            client_secret: "654321",
            scope:"all",
            redirect_uri:"http://user.cn"
        }, function (result) {
            $("#token").val(result.access_token)
        })
    }
    function logout(){
        location.href="http://sso.cn/logout?redirect_uri=http://user.cn&access_token="+$("#token").val();
    }

</script>

</html>